//个人中心
const path = require('path')
const db = require(path.join(__dirname, '../common/db.js'))
const express = require('express')
const utility = require('utility')
const router = express.Router()
//获取用户信息
router.get('/userinfo', async (req, res) => {
    //获取id
    let id = req.user.id
    // 根据id查询数据库
    let sql = 'select id, username, nickname, email, user_pic from user where id = ?'
    let ret = await db.operateDb(sql, id)
    if (ret.length > 0) {
        res.json({
            status: 0,
            message: '获取用户信息成功',
            data: ret[0]
        })
    } else {
        res.json({
            status: 1,
            message: '获取用户信息失败',
        })
    }
})
//更新密码
router.post('/updatepwd', async (req, res) => {
    //获取参数
    let params = req.body
    let id = req.user.id
    //输入的密码加密
    params.oldPwd = utility.md5(params.oldPwd)
    params.newPwd = utility.md5(params.newPwd)

    let sql = 'update user set password = ?  where id = ? and password = ?'
    let ret = await db.operateDb(sql, [params.newPwd, id, params.oldPwd])
    if (ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '重置密码成功'
        })
    } else {
        res.json({
            status: 1,
            message: '重置密码失败'
        })
    }
})
//更换头像
router.post('/update/avatar', async (req, res) => {
    //获取参数
    let params = req.body
    let id = req.user.id
    let sql = 'update user set  user_pic = ? where  id = ?'
    let ret = await db.operateDb(sql, [params.avatar, id])
    if (ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '更换头像成功'
        })
    } else {
        res.json({
            status: 1,
            message: '更换头像失败'
        })
    }
})
//更新用户信息
router.post('/userinfo', async (req, res) => {
    //获取参数
    let params = req.body
    // let id = req.user.id
    let sql = 'update user set ? where id = ?'
    let ret = await db.operateDb(sql, [{
        nickname: params.nickname,
        email: params.email
    }, params.id])
    if (ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '更换用户信息成功'
        })
    } else {
        res.json({
            status: 1,
            message: '更换用户信息失败'
        })
    }
})

//获取分类
router.get('/article/cates', async (req, res) => {
    //获取id
    // let id = req.user.id
    // 根据id查询数据库
    let sql = 'select id,name,alias  from cates where is_delete = ?'
    let ret = await db.operateDb(sql, 0)
    if (ret.length > 0) {
        res.json({
            status: 0,
            message: '获取文章分类成功',
            data: ret
        })
    } else {
        res.json({
            status: 1,
            message: '获取文章分类失败',
        })
    }
})

//添加分类
router.post('/addcates', async (req, res) => {
    let params = req.body

    //查询数据库中是否已经存在
    let csql = 'select id from cates where name = ?'
    let flag = await db.operateDb(csql, params.name)
    if (flag.length > 0) {
        res.json({
            status: 1,
            message: '该分类已存在',
        })
        return
    }
    //信息插入数据库
    let sql = 'insert into cates set ?'
    params.is_delete = 0;
    let ret = await db.operateDb(sql, params)
    if (ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '添加分类成功',
        })
    } else {
        res.json({
            status: 1,
            message: '添加分类失败',
        })
    }
})
//删除分类
router.get('/deletecate/:id', async (req, res) => {
    let id = req.params.id

    //信息插入数据库
    let sql = 'update cates set is_delete=1 where id=?'
    let ret = await db.operateDb(sql, id)
    if (ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '删除分类成功',
        })
    } else {
        res.json({
            status: 1,
            message: '删除分类失败',
        })
    }
})
//更新分类
router.post('/updatecate', async (req, res) => {
    let id = req.body.id
    //信息插入数据库
    let sql = 'update cates set ? where id=?'
    let ret = await db.operateDb(sql, [
        {
            name: req.body.name,
            alias: req.body.alias
        }, id
    ])
    if (ret.affectedRows > 0) {
        res.json({
            status: 0,
            message: '更新分类成功',
        })
    } else {
        res.json({
            status: 1,
            message: '更新分类失败',
        })
    }
})
module.exports = router